<?php
set_time_limit(0);
error_reporting(E_ALL);
require_once './Classes/PHPExcel.php';

$objPHPExcel = new PHPExcel();

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '用户ID')
            ->setCellValue('B1', '用户信息')
            ->setCellValue('C1', '手机号')
            ->setCellValue('D1', '备注')
            ->setCellValue('E1', '注册时间')
;
include '../c/config.php';
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);

if (!$mysqli->set_charset("utf8")) {}

if (isset($_GET['gid'])){
    $tableName='y'.$_GET['gid'];
}else{
$queryTable=$mysqli->query("SELECT id FROM `z_game` where is_open=1 limit 0,1 ");
$getTable=$queryTable->fetch_assoc();
$tableName='y'.$getTable['id'];
}
$where='';
if (isset($_GET['name']) && !empty($_GET['name'])){
    $where.=" id='".$_GET['name']."' or phone='".$_GET['name']."' or name like'%".$_GET['name']."%'  ";
    
}
if (isset($_GET['newold']) && !empty($_GET['newold'])){
    if (empty($where)){
        $where.=" beizhu='".$_GET['newold']."'  ";
    }else{
        $where.=" and beizhu='".$_GET['newold']."'  ";
    }
}
if (!empty($where)){
    $where=' where '.$where;
}

$queryUser=$mysqli->query("SELECT * FROM `".$tableName."_users`  ".$where."  ");
$index=2;
while ($list=$queryUser->fetch_assoc()){
    $objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A'.$index, $list['id'])
    ->setCellValue('B'.$index, base64_decode($list['nickname']))
    ->setCellValue('C'.$index, $list['phone'].' ')
    ->setCellValue('D'.$index, $list['beizhu'])
    ->setCellValue('E'.$index, $list['createtime'])
    ;
    $index++;
}
 
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('用户信息');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="用户信息.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
